﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using EffectsLib;

namespace MediaBrowser.CustomWindow
{
    /// <summary>
    /// Fenêtre personnalisé
    /// </summary>
    public class CustomWindowStyle : Window
    {
        private Line lnSizeNorth;
        private Line lnSizeSouth;
        private Line lnSizeWest;
        private Line lnSizeEast;

        private Rectangle rectSizeNorthWest;
        private Rectangle rectSizeNorthEast;
        private Rectangle rectSizeSouthWest;
        private Rectangle rectSizeSouthEast;

        /// <summary>
        /// Gestion d'un effet de gris, lorsque la propriétée de la fenêtre IsEnabled est modifié.
        /// </summary>
        private BrightContrastEffect brightContrastEffect;

        static CustomWindowStyle()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindowStyle), new FrameworkPropertyMetadata(typeof(CustomWindowStyle)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            lnSizeNorth = this.Template.FindName("lnSizeNorth", this) as Line;
            lnSizeSouth = this.Template.FindName("lnSizeSouth", this) as Line;
            lnSizeWest = this.Template.FindName("lnSizeWest", this) as Line;
            lnSizeEast = this.Template.FindName("lnSizeEast", this) as Line;

            rectSizeNorthWest = this.Template.FindName("rectSizeNorthWest", this) as Rectangle;
            rectSizeNorthEast = this.Template.FindName("rectSizeNorthEast", this) as Rectangle;
            rectSizeSouthWest = this.Template.FindName("rectSizeSouthWest", this) as Rectangle;
            rectSizeSouthEast = this.Template.FindName("rectSizeSouthEast", this) as Rectangle;

            brightContrastEffect = (this.Template.FindName("contentPresenter", this) as ContentPresenter).Effect as BrightContrastEffect;

            ComputeSize(this);
        }

        public bool HiddenMaximizeBox
        {
            get { return (bool)GetValue(HiddenMaximizeBoxProperty); }
            set { SetValue(HiddenMaximizeBoxProperty, value); }
        }

        public bool HiddenMinimumBox
        {
            get { return (bool)GetValue(HiddenMinimumBoxProperty); }
            set { SetValue(HiddenMinimumBoxProperty, value); }
        }

        #region dependency properties

        public static readonly DependencyProperty HiddenMaximizeBoxProperty =
          DependencyProperty.Register(
              "HiddenMaximizeBox", typeof(bool), typeof(CustomWindowStyle),
              new UIPropertyMetadata(false, new PropertyChangedCallback(HiddenMaximizeContentChanged)));

        public static readonly DependencyProperty HiddenMinimumBoxProperty =
           DependencyProperty.Register(
               "HiddenMinimumBox", typeof(bool), typeof(CustomWindowStyle));

        #endregion

        private static void HiddenMaximizeContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CustomWindowStyle c = d as CustomWindowStyle;
            ComputeSize(c);
        }

        private static void ComputeSize(CustomWindowStyle c)
        {
            if (c.lnSizeNorth != null)
            {
                if (c.HiddenMaximizeBox)
                {
                    c.lnSizeNorth.StrokeThickness = 0;
                    c.lnSizeSouth.StrokeThickness = 0;
                    c.lnSizeWest.StrokeThickness = 0;
                    c.lnSizeEast.StrokeThickness = 0;

                    c.rectSizeNorthWest.Width = 0;
                    c.rectSizeNorthEast.Width = 0;
                    c.rectSizeSouthWest.Width = 0;
                    c.rectSizeSouthEast.Width = 0;

                    c.rectSizeNorthWest.Height = 0;
                    c.rectSizeNorthEast.Height = 0;
                    c.rectSizeSouthWest.Height = 0;
                    c.rectSizeSouthEast.Height = 0;
                }
                else
                {
                    c.lnSizeNorth.StrokeThickness = 2;
                    c.lnSizeSouth.StrokeThickness = 2;
                    c.lnSizeWest.StrokeThickness = 2;
                    c.lnSizeEast.StrokeThickness = 2;

                    c.rectSizeNorthWest.Width = 5;
                    c.rectSizeNorthEast.Width = 5;
                    c.rectSizeSouthWest.Width = 5;
                    c.rectSizeSouthEast.Width = 5;

                    c.rectSizeNorthWest.Height = 5;
                    c.rectSizeNorthEast.Height = 5;
                    c.rectSizeSouthWest.Height = 5;
                    c.rectSizeSouthEast.Height = 5;
                }

                if ((c.HiddenMaximizeBox) && (c.HiddenMinimumBox))
                {
                    c.ResizeMode = ResizeMode.NoResize;
                }
                else if ((c.HiddenMaximizeBox) && (!c.HiddenMinimumBox))
                {
                    c.ResizeMode = ResizeMode.CanMinimize;
                }
                else
                {
                    c.ResizeMode = ResizeMode.CanResize;
                }
            }
        }

    }
}
